developing a hapijs edge by Van Nguyen Daniel Bretoi Wyatt Preul Lloyd Benson
Author:Van Nguyen, Daniel Bretoi, Wyatt Preul, Lloyd Benson
Language: eng
Format: epub
Publisher: Bleeding Edge Press
Published: 2015-07-02T00:00:00+00:00
NPM Downloads
To start, again, we configure another instantiable model class object NPMDownloads. Following the convention from before, we split the URL into host and parameterized URI functions. We know that all four API functions will basically be the same except for subtle changes to the URI, therefore, we define a shared method getDownloadsBy to be used by the other methods.
The lastYear’s URI is slightly more complicated than the rest, since we have calculated date ranges for today and a year prior to today. But it is not that complicated once you break it down.
var Hoek = require('hoek'); var Wreck = require('wreck'); var internals = {}; internals._defaults = { host: 'https://api.npmjs.org', uris: { 'last-day': function (_module) { return '/downloads/range/last-day/' + _module; }, 'last-week': function (_module) { return '/downloads/range/last-week/' + _module; }, 'last-month': function (_module) { return '/downloads/range/last-month/' + _module; }, 'last-year': function (_module) { var today = new Date(); var from = [today.getFullYear() - 1,today.getMonth + 1, today.getDate].join('-'); var to = [today.getFullYear(),today.getMonth + 1, today.getDate].join('-'); var dateRange = [from, to].join(':'); return '/downloads/range/' + dateRange + "/" + _module; } } }; internals.NpmDownloads = function (options) { this.options = Hoek.applyToDefaults(internals._defaults, options || {}); return this; }; internals.NpmDownloads.prototype.getDownloadsBy = function () {}; internals.NpmDownloads.prototype.lastDay = function () {}; internals.NpmDownloads.prototype.lastWeek = function () {}; internals.NpmDownloads.prototype.lastMonth = function () {}; internals.NpmDownloads.prototype.lastYear = function () {}; module.exports = internals.NpmDownloads;
Next, we fill out the shared function for calling the downloads request. But unlike the previous functions, this one returns a function that we use to generate the other requests. The other functions basically become wrappers for getDownloadsBy.
// ... internals.NpmDownloads.prototype.getDownloadsBy = function (_type, range) { var self = this; return function (moduleName, callback) { var method = "GET"; var uri = self.options.host + self.options.uris[range](moduleName); var options = { redirects: 3, timeout: 30000, rejectUnauthorized: true }; var processResponse = function (err, body) { // Leave room in case response modification required return callback(err, body); }; var readResponse = function (err, res) { if (err) { return callback(err); } Wreck.read(res, {json:true}, processResponse); }; var req = Wreck.request(method, uri, options, readResponse); }; }; // ...
Finally, we factor in getDownloadsBy for the remaining API methods by configuring only the differences between functions:
// ... internals.NpmDownloads.prototype.lastDay = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-day')(moduleName, callback); }; internals.NpmDownloads.prototype.lastWeek = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-week')(moduleName, callback); }; internals.NpmDownloads.prototype.lastMonth = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-month')(moduleName, callback); }; internals.NpmDownloads.prototype.lastYear = function (moduleName, callback) { return this.getDownloadsBy('point', 'last-year')(moduleName, callback); }; // ...
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Kotlin in Action by Dmitry Jemerov(13435)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(9908)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(9595)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3847)
Ember.js in Action by Joachim Haagen Skeie(3060)
Odoo 15 Development Essentials by Daniel Reis(2902)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2603)
Pride and Prejudice by Jane Austen(2443)
Learning Angular - Second Edition by Christoffer Noring(2432)
Computers For Seniors For Dummies by Nancy C. Muir(2112)
Mobile Forensics Cookbook by Igor Mikhaylov(2089)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1967)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1885)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1859)
1936941139 (N) by Bob Rosenthal(1838)
Hands-On Internet of Things with MQTT by Tim Pulver(1831)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1829)
Android App Development by Franceschi Hervé J.;(1819)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1726)